//
//  HLLiveChatView.m
//  YueYue
//
//  Created by hulu001 on 2024/3/12.
//

#import "HLLiveChatView.h"
#import "HLLiveMessageCell.h"

@interface HLLiveChatView ()<UITableViewDelegate, UITableViewDataSource>

@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) NSMutableArray *messageModels;  // tableview的数据源
@property (nonatomic, strong) NSMutableDictionary *msgIdDict;// 哈希存储消息ID与messageModel的映射

@end

@implementation HLLiveChatView


- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        _msgIdDict = [NSMutableDictionary dictionary];
        self.messageModels = [NSMutableArray array];
        [self setupUI];
    }
    return self;
}

#pragma mark - UI

- (void)setupUI
{
    self.tableView = [[UITableView alloc] init];
    self.tableView.backgroundColor = [UIColor.whiteColor colorWithAlphaComponent:0];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.estimatedRowHeight = 26;
    self.tableView.estimatedSectionHeaderHeight = 0;
    self.tableView.estimatedSectionFooterHeight = 0;
    self.tableView.contentInset = UIEdgeInsetsMake(0, 0, 20, 0);
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.tableView.tableFooterView = [UIView.alloc initWithFrame:CGRectZero];
    [self.tableView registerClass:[HLLiveMessageCell class] forCellReuseIdentifier:[HLLiveMessageCell identifyCell]];
    [self addSubview:self.tableView];
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.mas_equalTo(0);
        make.height.mas_equalTo(80);
    }];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    HLLiveMessageCell *cell = [tableView dequeueReusableCellWithIdentifier:[HLLiveMessageCell identifyCell]];
    HLLiveMessageModel *message =  self.messageModels[indexPath.row];
    message.index = indexPath.row;
    cell.messageModel = message;
    return cell;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.messageModels.count;
}

#pragma mark - lyout

- (void)adjustTableView
{
//    if (self.tableView.contentSize.height < self.frame.size.height) {
//        self.tableView.frame = CGRectMake(0, self.frame.size.height - self.tableView.contentSize.height, self.tableView.frame.size.width, self.tableView.contentSize.height);
//
//        NSLog(@"tableView.frame = %@", NSStringFromCGRect(self.tableView.frame));
//        NSLog(@"tableView.contentSize = %@", NSStringFromCGSize(self.tableView.contentSize));
//    } else {
//        self.tableView.frame = self.bounds;
//
//        NSInteger row = [self.tableView numberOfRowsInSection:0];
//        [self.tableView scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:row-1 inSection:0] atScrollPosition:UITableViewScrollPositionBottom animated:YES];
//    }
    
    [self.tableView mas_updateConstraints:^(MASConstraintMaker *make) {
        make.height.mas_equalTo(self.tableView.contentSize.height + 60);
    }];
    
    NSInteger row = [self.tableView numberOfRowsInSection:0];
    [self.tableView scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:row-1 inSection:0] atScrollPosition:UITableViewScrollPositionBottom animated:YES];
}

#pragma mark - Public

- (void)addMessage:(HLLiveMessageModel *)message
{
    /*
     1、过滤消息
     2、加入到消息处理队列
     */
    
    // 已经存在该条消息
    if ([_msgIdDict objectForKey:message.messageId]) {
        return;
    }
    
    // 追加到数据源
    [self.messageModels addObject:message];
    [_msgIdDict setObject:message forKey:message.messageId];
    
    // 插入到tableview
    [self.tableView beginUpdates];
    NSIndexPath *indexPath = [NSIndexPath indexPathForRow:self.messageModels.count-1 inSection:0];
    [self.tableView insertRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationNone];
    [self.tableView endUpdates];
    
    // layout
    [self adjustTableView];
}

- (void)updateMessage:(HLLiveMessageModel *)message
{

    
    // 3、更新MessageModel
    NSInteger index = [self.messageModels indexOfObject:message];
    
    // 4、刷新
    HLLiveMessageCell *cell = [self.tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:index inSection:0]];
    if (cell) {
        [self.tableView reloadRowsAtIndexPaths:@[[NSIndexPath indexPathForRow:index inSection:0]] withRowAnimation:UITableViewRowAnimationNone];
        
        // 如果更新的是最后一个消息，自动滚动到底部
        if (index == [self.tableView numberOfRowsInSection:0]-1) {
            [self adjustTableView];
        }
    }
}



@end
